﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Net;

namespace iData
{
    #region 存储过程类
    public class DataStoreAcess
    {
        SqlCommand _command = new SqlCommand();
        public DataStoreAcess()
        {
            _command.Parameters.Clear();
            _command.CommandType = CommandType.StoredProcedure;
            _command.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4));
            _command.Parameters["@RETURN_VALUE"].Direction = ParameterDirection.ReturnValue;

        }
        //常用函数实现区域
        public SqlParameter AddParam(string ParamName, SqlDbType ParamType)
        {
            SqlParameter param = new SqlParameter(ParamName, ParamType);
            _command.Parameters.Add(param);
            return param;
        }
        public SqlParameter AddParam(string ParamName, SqlDbType ParamType, int Len)
        {
            SqlParameter param = new SqlParameter(ParamName, ParamType, Len);
            _command.Parameters.Add(param);
            return param;
        }
        public SqlParameter AddParam(string ParamName, SqlDbType ParamType, bool bOut)
        {
            SqlParameter param = new SqlParameter(ParamName, ParamType);
            param.Direction = ParameterDirection.InputOutput;
            _command.Parameters.Add(param);
            return param;

        }

        public SqlParameter AddParam(string ParamName, SqlDbType ParamType, int len, bool bOut)
        {
            SqlParameter param = new SqlParameter(ParamName, ParamType, len);
            param.Direction = ParameterDirection.InputOutput;
            _command.Parameters.Add(param);
            return param;

        }

        public DataSet Execute()
        {
            SqlConnection con = new SqlConnection();

            DataSet dataSet = null;
            try
            {
                con.ConnectionString = _conString;
                con.Open();
                _command.Connection = con;
                _command.CommandText = _prcName;
                SqlDataAdapter adapter = new SqlDataAdapter(_command);
                dataSet = new DataSet();
                adapter.Fill(dataSet);


            }
            catch (System.Exception ex)
            {
                dataSet = null;
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return dataSet;

        }
        public int ExecuteNonQuery()
        {
            SqlConnection con = new SqlConnection();
            int RowCount = -1;
            try
            {

                con.ConnectionString = _conString;
                con.Open();
                _command.CommandText = _prcName;
                _command.Connection = con;
                RowCount = _command.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {

                throw ex;
            }
            finally
            {
                con.Close();
            }
            return RowCount;


        }


        public DataSet Execute(SqlTransaction trans)
        {

            DataSet dataSet = null;
            try
            {
                _command.CommandText = _prcName;
                _command.Connection = trans.Connection;
                _command.Transaction = trans;
                SqlDataAdapter adapter = new SqlDataAdapter(_command);
                dataSet = new DataSet();
                adapter.Fill(dataSet);


            }
            catch (System.Exception ex)
            {
                dataSet = null;
                throw ex;
            }

            return dataSet;

        }
        public int ExecuteNonQuery(SqlTransaction trans)
        {

            int RowCount = -1;
            try
            {
                _command.CommandText = _prcName;
                _command.Connection = trans.Connection;
                _command.Transaction = trans;
                RowCount = _command.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {

                throw ex;
            }

            return RowCount;


        }
        // 属性实现区域

        string _prcName = "";
        public string PrcName
        {
            get { return _prcName; }
            set
            {
                _prcName = value;
                _command.CommandText = _prcName;
            }
        }
        string _conString = "";
        public string ConnectStr
        {
            get { return _conString; }
            set { _conString = value; }
        }
        //只读属性
        public SqlParameterCollection Parameters
        {
            get { return _command.Parameters; }
        }

    }
    #endregion

}
